-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Merge sub properties in add_post_type_support().
#10426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Merge sub properties in add_post_type_support().
#10426
Conversation
Previously, calling `add_post_type_support()` multiple times for the same post type and feature with array arguments would overwrite the previous arguments instead of merging them. This prevented plugins and themes from incrementally adding sub-properties to the same feature.
This changes the behavior to merge associative array arguments when the function is called multiple times with the same post type and feature combination.
Example usage:
{{{
add_post_type_support( 'page', 'editor', array(
'default-mode' => 'template-locked',
) );
add_post_type_support( 'page', 'editor', array(
'block-comments' => true,
) );
// Both properties are now preserved instead of only the last one.
}}}
The fix maintains full backwards compatibility by only merging when both the existing and new values are associative arrays. All other calling patterns continue to work as before.
Props fabiankaegy, mamaduka, wildworks.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
adamsilverstein
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
|
IMO, the current behavior is broken, but given everything WP, would this be considered a breaking change? |
Co-authored-by: Mamaduka <[email protected]>
src/wp-includes/post.php
Outdated
| if ( isset( $_wp_post_type_features[ $post_type ][ $feature ][0] ) | ||
| && is_array( $_wp_post_type_features[ $post_type ][ $feature ][0] ) | ||
| && isset( $args[0] ) | ||
| && is_array( $args[0] ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The is_array( $args[0] ) call here isn't checking whether the arg is an associative array. Shouldn't it also check if ! isset( $args[0][0] )?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or rather: ! array_is_list( $args[0] )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Additionally, what if someone tries to merge an empty array? Should that be allowed? So perhaps the new check here should be: ! array_is_list( $args[0] ) || empty( $args[0] )
src/wp-includes/post.php
Outdated
| $_wp_post_type_features[ $post_type ][ $feature ] = $args; | ||
| // Check if feature already exists with args and if both are associative arrays that should be merged. | ||
| if ( isset( $_wp_post_type_features[ $post_type ][ $feature ][0] ) | ||
| && is_array( $_wp_post_type_features[ $post_type ][ $feature ][0] ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per below, should this check to make sure it is actually an associative array? I think it should add this check: ! array_is_list( $_wp_post_type_features[ $post_type ][ $feature ][0] )
Co-authored-by: Weston Ruter <[email protected]>
|
The RC1 release is approaching, so we need to decide whether or not to ship this pull request. What do you think? |
| * | ||
| * @ticket 64156 | ||
| */ | ||
| public function test_add_post_type_support_merges_array_arguments() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: Also add tests for theme features merging non-associative arrays.
Co-authored-by: Weston Ruter <[email protected]>
Co-authored-by: Weston Ruter <[email protected]>
Co-authored-by: Weston Ruter <[email protected]>
Co-authored-by: Weston Ruter <[email protected]>
|
This pull request seems very robust, but my only concern is that it behaves differently from |
For completeness it seems that being able to remove sub-features via function add_post_type_support( $post_type, $feature, ...$args )So: function remove_post_type_support( $post_type, $feature, ...$args )But how would this be used? If |
I believe this need exists because the Consider a scenario where multiple sub-functions are registered as shown below, and you want to disable only the 'notes' sub-feature: add_post_type_support( 'custom', 'editor', array(
'default-mode' => 'template-locked',
'notes' => true,
) );You will need to remove the existing main feature and then register the necessary sub-feature again, as follows. remove_post_type_support( 'custom', 'editor' );
add_post_type_support( 'custom', 'editor', array(
'default-mode' => 'template-locked',
) );I don't think this concern will be a blocker here, but I wanted to share it just in case. |
|
Consider also that add_post_type_support( 'post', array( 'title', 'author' ), array( 'jjj' => 'cool' ) );...and that I think using Is it too late to create a new feature key instead? |
I think this was/is an "issue" even without this patch.
Agreed, it makes sense to have feature parity here. We should also update
Initially, it was implemented this way, but we changed it later, since these are the actual sub-features for cc @swissspidy |
| * @since 3.0.0 | ||
| * @since 5.3.0 Formalized the existing and already documented `...$args` parameter | ||
| * by adding it to the function signature. | ||
| * @since 6.9.0 Multiple calls to add support for the same feature with array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As per my comment on the ticket, I'd say this is something for 7.0. It's an enhancement and could have unintended side effects for people who relied on the previous behavior, and we're about to publish RC1.
| $this->assertTrue( $support['editor'][0]['block-comments'] ); | ||
| $this->assertSame( 'test-value', $support['editor'][0]['another-option'] ); | ||
|
|
||
| _unregister_post_type( 'foo' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just noting that this code will never run if any of the above assertions fails. Best to move this to tear_down() or restructure the test to call this before the assertions.
| ); | ||
|
|
||
| $support = get_all_post_type_supports( 'foo' ); | ||
| $this->assertIsArray( $support['editor'] ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The best practice in core is to add custom assertion messages (the last argument) when a test contains numerous assertions. Makes it much easier to debug a failing test and figuring out which assertion is actually failing.
Previously, calling
add_post_type_support()multiple times for the same post type and feature with array arguments would overwrite the previous arguments instead of merging them. This prevented plugins and themes from incrementally adding sub-properties to the same feature.This changes the behavior to merge associative array arguments when the function is called multiple times with the same post type and feature combination.
Example usage:
The fix maintains full backwards compatibility by only merging when both the existing and new values are associative arrays. All other calling patterns continue to work as before.
Props @fabiankaegy, @Mamaduka, @t-hamano .
Trac ticket: https://core.trac.wordpress.org/ticket/64156#ticket
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.